package com.start.manager.controller;


import com.start.manager.domain.entity.Detail;
import com.start.manager.domain.vo.DetailVO;
import com.start.manager.service.IDetailService;
import com.start.manager.util.PageUtil;
import com.start.manager.util.ResponseResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Lijian
 * @since 2019-12-20
 */
@RestController
@RequestMapping("/detail")
public class DetailController {

    @Autowired
    private IDetailService detailService;

    /**
     * 查询分页获取
     * @param detailVO
     * @return
     */
    @PostMapping(value = "/getList")
    @ResponseBody
    public ResponseResult getList(@RequestBody DetailVO detailVO){
        PageUtil pageUtil =  detailService.findList(detailVO);
        return ResponseResult.success(pageUtil);
    }

    /**
     * 新增和更新
     * @param detailVO
     * @return
     */
    @PostMapping(value="/saveDetail")
    @ResponseBody
    public ResponseResult saveGoods(@RequestBody DetailVO detailVO) {
        Detail byId = null;
        if (StringUtils.isNotBlank(detailVO.getId())){
            byId = detailService.getById(detailVO.getId());
        }
        if (byId == null){
            boolean result = detailService.saveDetail(detailVO);
            if (!result){
                return ResponseResult.error("保存失败");
            }else{
                return ResponseResult.success("保存成功");
            }
        }else{
            //更新用户操作
            BeanUtils.copyProperties(detailVO,byId);
            boolean result = detailService.updateById(byId);
            if (!result){
                return ResponseResult.error("更新失败");
            }else{
                return ResponseResult.success("更新成功");
            }
        }


    }

    /**
     * 删除
     * @param detailId
     * @return
     */
    @DeleteMapping(value="/deleteDetail/{detailId}")
    @ResponseBody
    public ResponseResult deleteGoods(@PathVariable String detailId){
//        Detail byId = detailService.getById(detailId);
//        if (byId == null){
//            return ResponseResult.error("该记录不存在");
//        }
//        Detail detail = byId.setStatus(StatusConstant.NO_USE);
//        boolean result = detailService.updateById(detail);
        boolean result = detailService.removeById(detailId);
        if (!result){
            return ResponseResult.error();
        }
        return ResponseResult.success();
    }

}
